
package com.kugou.game.sdk.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kugou.game.sdk.api.common.ActivityOrientation;
import com.kugou.game.sdk.core.PayManager;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.utils.Rx;

/**
 * 充值界面金额选择适配器
 * 
 * @author ljc
 * @since 2013-12-18 上午11:21:32
 */
public class RechargeMoneyGridAdapter extends BaseAdapter {

    private static final int LEFT_TOP = 0;

    private static final int LEFT_TOP_PRESSED = 5;

    private static final int LEFT_BOTTOM = 3;

    private static final int LEFT_BOTTOM_PRESSED = 8;

    private static final int TOP_MIDDLE = 1;

    private static final int MIDDLE_PRESSED = 6;

    private static final int RIGHT_TOP = 2;

    private static final int RIGHT_TOP_PRESSED = 7;

    private static final int RIGHT_BOTTOM = 4;

    private static final int RIGHT_BOTTOM_PRESSED = 9;

    private static final int LEFT_LAND = 10;

    private static final int RIGHT_LAND = 11;

    private static final int LEFT_LAND_PRESSED = 12;

    private static final int RIGHT_LAND_PRESSED = 13;

    private static final int BOTTOM_MIDDLE = 14;

    private static final int MIDDLE_LAND = 15;

    public static final int UNIT_YUN = 0;

    public static final int UNIT_KUBI = 1;

    private Context context;

    private int[] mMoneys;

    private int mMoneyIndex = -1;

    private int mUnit = UNIT_YUN;

    public RechargeMoneyGridAdapter(Context context, int[] mMoneys, int unit) {
        this(context, mMoneys);
        this.mUnit = unit;
    }

    private int[] mResourceId;

    public RechargeMoneyGridAdapter(Context context, int[] mMoneys) {
        this.context = context;
        this.mMoneys = mMoneys;
        initResourceId();
    }

    private void initResourceId() {
        mResourceId = new int[] {
                Rx.drawable.kg_recharge_gridview_itemlefttop,
                Rx.drawable.kg_recharge_gridview_itemmiddletop,
                Rx.drawable.kg_recharge_gridview_itemrighttop,
                Rx.drawable.kg_recharge_gridview_itemleftbottom,

                Rx.drawable.kg_recharge_gridview_itemrightbottom,

                Rx.drawable.kg_shape_recharge_gridview_itemlefttop_pressed,

                Rx.drawable.kg_shape_recharge_gridview_itemmiddle_pressed,

                Rx.drawable.kg_shape_recharge_gridview_itemrighttop_pressed,

                Rx.drawable.kg_shape_recharge_gridview_itemleftbottom_pressed,

                Rx.drawable.kg_shape_recharge_gridview_itemrightbottom_pressed,
                Rx.drawable.kg_recharge_gridview_itemleft_land,
                Rx.drawable.kg_recharge_gridview_itemright_land,

                Rx.drawable.kg_shape_recharge_gridview_itemleft_pressed,

                Rx.drawable.kg_shape_recharge_gridview_itemright_pressed,

                Rx.drawable.kg_recharge_gridview_itemmiddlebottom,

                Rx.drawable.kg_recharge_gridview_itemmiddle_land
        };

    }

    public void changeMoneyType(int[] mMoney) {
        this.mMoneys = mMoney;
    }

    @Override
    public int getCount() {
        return mMoneys != null ? mMoneys.length : 0;
    }

    @Override
    public Integer getItem(int position) {
        return mMoneys[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Cache cache;
        if (convertView == null) {
            cache = new Cache();
            int resLayoutId = Rx.layout.kg_card_money_cell;
            convertView = ((Activity) context).getLayoutInflater().inflate(resLayoutId, null);
            int resId = Rx.id.kg_card_money_tv;
            cache.textView = (TextView) convertView.findViewById(resId);
            convertView.setTag(cache);
        } else {
            cache = (Cache) convertView.getTag();
        }
        if (cache.textView != null) {
            if (mMoneys[position] != 0) {
                // update by allen 8-25 11:56
                if (mUnit == UNIT_KUBI)
                    cache.textView.setText(String.valueOf(mMoneys[position]
                            * PayManager.getYoubiRatio())
                            + "游币");
                else
                    cache.textView.setText(String.valueOf(mMoneys[position]) + "元");
            } else {
                cache.textView.setText("");
            }

            int selectorId = 0;
            int selectorTurnId = 0;

            if (SDKControler.getActivityOrientation() == ActivityOrientation.LANDSCAPE) {
                if (position == 0) {
                    // 左角
                    selectorId = mResourceId[LEFT_LAND];
                    selectorTurnId = mResourceId[LEFT_LAND_PRESSED];
                } else if (position == 5) {
                    // 右角
                    selectorId = mResourceId[RIGHT_LAND];
                    selectorTurnId = mResourceId[RIGHT_LAND_PRESSED];
                } else {
                    // 中间
                    selectorId = mResourceId[MIDDLE_LAND];
                    selectorTurnId = mResourceId[MIDDLE_PRESSED];
                }
            } else {
                if (position == 0) {
                    // 左上角
                    selectorId = mResourceId[LEFT_TOP];
                    selectorTurnId = mResourceId[LEFT_TOP_PRESSED];
                } else if (position == 3) {
                    // 左下角
                    selectorId = mResourceId[LEFT_BOTTOM];
                    selectorTurnId = mResourceId[LEFT_BOTTOM_PRESSED];
                } else if (position == 2) {
                    // 右上角
                    selectorId = mResourceId[RIGHT_TOP];
                    selectorTurnId = mResourceId[RIGHT_TOP_PRESSED];
                } else if (position == 5) {
                    // 右下角
                    selectorId = mResourceId[RIGHT_BOTTOM];
                    selectorTurnId = mResourceId[RIGHT_BOTTOM_PRESSED];
                } else if (position == 1) {
                    // 中间(上)
                    selectorId = mResourceId[TOP_MIDDLE];
                    selectorTurnId = mResourceId[MIDDLE_PRESSED];
                } else {
                    // 中间下
                    selectorId = mResourceId[BOTTOM_MIDDLE];
                    selectorTurnId = mResourceId[MIDDLE_PRESSED];
                }
            }

            if (position == mMoneyIndex) {
                cache.textView.setTextColor(context.getResources()
                        .getColor(Rx.color.kg_common_blue));
                cache.textView.setBackgroundResource(selectorTurnId);
            } else {
                cache.textView.setTextColor(context.getResources().getColor(
                        Rx.color.kg_common_gray_darker));
                cache.textView.setBackgroundResource(selectorId);
            }
        }
        return convertView;
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mMoneyIndex = position;
    }

    public int getmMoneyIndex() {
        return mMoneyIndex;
    }

    public void setmMoneyIndex(int mMoneyIndex) {
        this.mMoneyIndex = mMoneyIndex;
    }

    class Cache {
        TextView textView;
    }

}
